package me.yricky.cov_spectrum.ui.theme

import android.annotation.SuppressLint
import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import me.yricky.cov_spectrum.R

private val DarkColorPalette = darkColors(
    primary = Color(0xff5588aa),
    primaryVariant = Color(0xff5588aa),
    secondary = Color(0xff66cc99)
)

private val LightColorPalette = lightColors(
    primary = Color(0xff5588aa),
    primaryVariant = Color(0xff5588aa),
    secondary = Color(0xff66cc99)

    /* Other default colors to override
    background = Color.White,
    surface = Color.White,
    onPrimary = Color.White,
    onSecondary = Color.Black,
    onBackground = Color.Black,
    onSurface = Color.Black,
    */
)

@Composable
fun TopBar(
    modifier: Modifier = Modifier,
    content: @Composable RowScope.() -> Unit,
    tailIcon: @Composable RowScope.() ->Unit,
){
    TopAppBar(
        modifier, elevation = 0.dp,
        backgroundColor = MaterialTheme.colors.primary,
    ) {
        Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {
            content(this)
        }
        tailIcon(this)
    }
}

@Composable
fun LoadingPage(modifier: Modifier = Modifier){
    Box(modifier = modifier){
        CircularProgressIndicator(
            Modifier
                .size(48.dp)
                .align(Alignment.Center))
    }
}

@Composable
fun ErrorPage(modifier: Modifier = Modifier,e:Throwable){
    Box(modifier = modifier){
        Box(Modifier.fillMaxSize()){
            Text(
                modifier = Modifier.align(Alignment.Center),
                text = e.localizedMessage ?: "Unknown error"
            )
        }
    }
}

@Composable
fun EmptyPage(modifier: Modifier = Modifier,msg:String = "empty"){
    Box(modifier = modifier){
        Column(Modifier.align(Alignment.Center), horizontalAlignment = Alignment.CenterHorizontally) {
            Icon(
                painter = painterResource(id = R.drawable.ic_round_inbox_24),
                contentDescription = "",
                modifier = Modifier.size(48.dp)
            )
            Text(text = msg)
        }
    }
}

@SuppressLint("ModifierParameter")
@Composable
fun IconButton(
    modifier: Modifier = Modifier.size(40.dp),
    painter:Painter,
    tint:Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
    contentDescription: String? = null,
    onClick:()->Unit,
){
    Surface(
        modifier = modifier,
        color = Color.Transparent,
        shape = RoundedCornerShape(48.dp)
    ) {
        Icon(
            painter = painter,
            contentDescription = contentDescription,
            tint = tint,
            modifier = Modifier
                .clickable {
                    onClick()
                }
                .fillMaxSize()
                .padding(4.dp)
        )
    }
}

@Composable
fun CovspectrumTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) {
    val colors = if (darkTheme) {
        DarkColorPalette
    } else {
        LightColorPalette
    }

    MaterialTheme(
        colors = colors,
        typography = Typography,
        shapes = Shapes,
        content = content
    )
}